class_name RoundAutoOverSys
extends System

const CATCH_READ = ["FrameTime", "SkillAfterDelay", "RoundFighter", "RoundStateC"]

var catch_read := []
var dirty = 0
var round_delay = 0


func _init(_round_delay = 4):
	round_delay = _round_delay
	system_name = "RoundAutoOverSys"
	listen_modify("on_round_start", "RoundStateC", ["RoundStateC", "FrameTime", "SkillAfterDelay"])


func _system_ready():
	catch_read = world.query_manager.fetch_components(CATCH_READ)


func _system_process(_d):
	if not dirty:
		return
	var read = catch_read
	var frame = read[0].data as FrameTime
	var after_delay = read[1].data as SkillAfterDelay
	var round_fighter = read[2].data as RoundFighter
	var round_state_m = read[3] as Multi  #Multi<RoundStateC>
	if frame.count <= after_delay.value:
		return
	round_fighter.value = 0
	var round_state = round_state_m.get(dirty) as RoundState
	dirty = 0
	if not round_state:
		return
	round_state.value = 0


#@listenModify(RoundStateC)
#read:[Multi<RoundStateC>, Single<FrameTime>, Single<SkillAfterDelay>]
func on_round_start(e, read: Array):
	var round_state_m = read[0] as Multi  #Multi<RoundStateC>
	var frame = read[1].data as FrameTime
	var after_delay = read[2].data as SkillAfterDelay

	var round_state = round_state_m.get(e.id) as RoundState
	if round_state.value == 1:
		dirty = e.id
		after_delay.value = frame.count + round_delay
